package com.ling.recipeassistant.data.repository

import com.ling.recipeassistant.data.local.RecipeDao
import com.ling.recipeassistant.data.model.Recipe
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class RecipeRepository @Inject constructor(
    private val recipeDao: RecipeDao
) {
    
    fun getAllRecipes(): Flow<List<Recipe>> = recipeDao.getAllRecipes()
    
    fun getFavoriteRecipes(): Flow<List<Recipe>> = recipeDao.getFavoriteRecipes()
    
    fun getRecipesByCategory(category: String): Flow<List<Recipe>> = 
        recipeDao.getRecipesByCategory(category)
    
    fun searchRecipes(query: String): Flow<List<Recipe>> = 
        recipeDao.searchRecipes(query)
    
    fun getRecipesByIngredient(ingredient: String): Flow<List<Recipe>> = 
        recipeDao.getAllRecipesForIngredientSearch().map { recipes ->
            recipes.filter { recipe ->
                recipe.ingredients.any { it.name.contains(ingredient, ignoreCase = true) }
            }
        }
    
    suspend fun getRecipeById(recipeId: String): Recipe? = 
        recipeDao.getRecipeById(recipeId)
    
    suspend fun insertRecipe(recipe: Recipe) = recipeDao.insertRecipe(recipe)
    
    suspend fun insertRecipes(recipes: List<Recipe>) = recipeDao.insertRecipes(recipes)
    
    suspend fun updateRecipe(recipe: Recipe) = recipeDao.updateRecipe(recipe)
    
    suspend fun updateFavoriteStatus(recipeId: String, isFavorite: Boolean) = 
        recipeDao.updateFavoriteStatus(recipeId, isFavorite)
    
    suspend fun deleteRecipe(recipe: Recipe) = recipeDao.deleteRecipe(recipe)
    
    // 获取所有分类
    fun getAllCategories(): Flow<List<String>> = 
        recipeDao.getAllRecipes().map { recipes ->
            recipes.map { it.category }.distinct().sorted()
        }
    
    // 获取随机菜谱
    fun getRandomRecipes(limit: Int = 10): Flow<List<Recipe>> = 
        recipeDao.getAllRecipes().map { recipes ->
            recipes.shuffled().take(limit)
        }
    
    // 根据难度获取菜谱
    fun getRecipesByDifficulty(difficulty: String): Flow<List<Recipe>> = 
        recipeDao.getAllRecipes().map { recipes ->
            recipes.filter { it.difficulty.name == difficulty }
        }
    
    // 获取高评分菜谱
    fun getTopRatedRecipes(limit: Int = 10): Flow<List<Recipe>> = 
        recipeDao.getAllRecipes().map { recipes ->
            recipes.sortedByDescending { it.rating }.take(limit)
        }
    
    // 获取快速菜谱（烹饪时间少于30分钟）
    fun getQuickRecipes(): Flow<List<Recipe>> = 
        recipeDao.getAllRecipes().map { recipes ->
            recipes.filter { it.cookTime <= 30 }
        }
    
    // 收藏相关方法
    suspend fun toggleFavorite(recipeId: String) {
        val recipe = getRecipeById(recipeId)
        recipe?.let {
            updateFavoriteStatus(recipeId, !it.isFavorite)
        }
    }
    
    suspend fun addToFavorites(recipeId: String) {
        updateFavoriteStatus(recipeId, true)
    }
    
    suspend fun removeFromFavorites(recipeId: String) {
        updateFavoriteStatus(recipeId, false)
    }
    
    suspend fun isFavorite(recipeId: String): Flow<Boolean> =
        recipeDao.getRecipeById(recipeId).let { recipe ->
            recipe?.let { kotlinx.coroutines.flow.flowOf(it.isFavorite) } 
                ?: kotlinx.coroutines.flow.flowOf(false)
        }
} 